import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import * as commonFunc from '../../Common/commonFunc';

class WorkOrderPpopComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: '', /* 配置 */
      masterShowConfig: '', /* 表配置 */
      loading: false,
      formData: props.formData,
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      dPartsQty: '',
      iPage: '',
      iPrintModePo: '',
      iPrintMode: '',
      sPrintModeMemo: '',
      dSinglePQty: '',
      dMaterialsKQty: '',
      iPositiveColor: '',
      iPositiveSpecialColor: '',
      iOppositeColor: '',
      iOppositeSpecialColor: '',
      dPlateQty: '',
      dSumPlateQty: '',
      dMachineWidth: '',
      dMachineLength: '',
      dMachineQty: '',
      dStretchPrintQty: '',
      dStretchPrintAfterQty: '',
      dSumStretchQty: '',
      dSumMachineQty: '',
      colorVisible: false, /* 正面反面专色是否显示 */
      workOrderJigSawData: '', /* 拼版 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const { formData } = this.state;
    const workOrderPpopConfig = formData.filter(item => (item.sTbName.toLowerCase() === 'mftproductcontrol' && item.sSqlStr === undefined))[0];
    /* 窗体显示配置 */
    const masterShowConfig = commonFunc.getConfigCondition(workOrderPpopConfig.gdsconfigformslave, 'bVisible');
    let workOrderJigSawData = '';
    if (this.props.workOrderJigsawSelectKey !== '') {
      [workOrderJigSawData] = this.props.workOrderJigSawData.filter(item => (item.sId !== undefined ? item.sId === this.props.workOrderJigsawSelectKey : item.key === this.props.workOrderJigsawSelectKey));
    }
    if (workOrderPpopConfig.gdsconfigformslave !== undefined) {
      /* 工单主表配置 */
      const mainConfig = workOrderPpopConfig.gdsconfigformslave;
      this.setState({
        masterConfig: workOrderPpopConfig,
        masterShowConfig,
        workOrderJigSawData,
        iPage: mainConfig.filter(item => (item.sName.toLowerCase() === 'ipage'))[0],
        iPrintModePo: mainConfig.filter(item => (item.sName.toLowerCase() === 'iprintmodepo'))[0],
        iPrintMode: mainConfig.filter(item => (item.sName.toLowerCase() === 'iprintmode'))[0],
        sPrintModeMemo: mainConfig.filter(item => (item.sName.toLowerCase() === 'sprintmodememo'))[0],
        iPositiveColor: mainConfig.filter(item => (item.sName.toLowerCase() === 'ipositivecolor'))[0],
        iPositiveSpecialColor: mainConfig.filter(item => (item.sName.toLowerCase() === 'ipositivespecialcolor'))[0],
        iOppositeColor: mainConfig.filter(item => (item.sName.toLowerCase() === 'ioppositecolor'))[0],
        iOppositeSpecialColor: mainConfig.filter(item => (item.sName.toLowerCase() === 'ioppositespecialcolor'))[0],
        dMachineWidth: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmachinewidth'))[0],
        dMachineLength: mainConfig.filter(item => (item.sName.toLowerCase() === 'dmachinelength'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    let workOrderJigSawData = '';
    if (nextProps.workOrderJigsawSelectKey !== '') {
      [workOrderJigSawData] = nextProps.workOrderJigSawData.filter(item => (item.sId !== undefined ? item.sId === nextProps.workOrderJigsawSelectKey : item.key === nextProps.workOrderJigsawSelectKey));
    }
    this.setState({ workOrderJigSawData, disabled: nextProps.disabled });
  }

  /* 数据自动变更 */
  handleFormChange = (value) => {
    this.props.handlePpopJigSawData(value);
  };

  render() {
    const { props, state } = this;
    return (
      <WorkOrderInfoComponent
        {...props}
        {...state}
        onChange={this.handleFormChange.bind(this)}
      />
    );
  }
}
const WorkOrderInfoComponent = Form.create({
  mapPropsToFields(props) {
    const { workOrderJigSawData, masterShowConfig } = props;
    const obj = commonFunc.mapPropsToFieldsChangeValue(masterShowConfig, workOrderJigSawData);
    return obj;
  },
  onValuesChange(props, changedFields) {
    props.onChange(changedFields);
  },
})((props) => {
  const {
    app, form, disabled, handleModalPane, dataName, dataType,
  } = props;
  const childProps = {
    app,
    form,
    disabled,
    handleModalPane,
    dataName,
    dataType,
  };
  const {
    iPage, iPrintModePo, iPrintMode, sPrintModeMemo,
    iPositiveColor, iPositiveSpecialColor, iOppositeColor,
    iOppositeSpecialColor, dMachineWidth, dMachineLength,
  } = props;
  const iPageProps = { showConfig: iPage, ...childProps };
  const iPrintModePoProps = { showConfig: iPrintModePo, ...childProps };
  const iPrintModeProps = { showConfig: iPrintMode, ...childProps };
  const sPrintModeMemoProps = { showConfig: sPrintModeMemo, ...childProps };
  const iPositiveColorProps = { showConfig: iPositiveColor, ...childProps };
  const iPositiveSpecialColorProps = { showConfig: iPositiveSpecialColor, ...childProps };
  const iOppositeColorProps = { showConfig: iOppositeColor, ...childProps };
  const iOppositeSpecialColorProps = { showConfig: iOppositeSpecialColor, ...childProps };
  const dMachineWidthProps = { showConfig: dMachineWidth, ...childProps };
  const dMachineLengthProps = { showConfig: dMachineLength, ...childProps };
  return (
    <Form>
      <Row type="flex">
        <Col
          span={6}
          order={iPage.iOrder > 24 ? 24 : iPage.iOrder}
          style={{ display: iPage.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="iPage" showDefaultName="倍率" {...iPageProps} />
        </Col>
        <Col
          span={6}
          order={iPrintModePo.iOrder > 24 ? 24 : iPrintModePo.iOrder}
          style={{ display: iPrintModePo.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="iPrintModePo" showDefaultName="印刷面数" {...iPrintModePoProps} />
        </Col>
        <Col
          span={6}
          order={sPrintModeMemo.iOrder > 24 ? 24 : sPrintModeMemo.iOrder}
          style={{ display: sPrintModeMemo.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="sPrintModeMemo" showDefaultName="1付左右翻" {...sPrintModeMemoProps} textArea />
        </Col>
        <Col
          span={6}
          order={iPositiveColor.iOrder > 24 ? 24 : iPositiveColor.iOrder}
          style={{ display: iPositiveColor.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="iPositiveColor" showDefaultName="正面颜色" {...iPositiveColorProps} />
        </Col>
        <Col
          span={6}
          order={iOppositeColor.iOrder > 24 ? 24 : iOppositeColor.iOrder}
          style={{ display: 'none' }}
        >
          <ShowType sName="iOppositeColor" showDefaultName="反面颜色" {...iOppositeColorProps} bVisible={false} />
        </Col>
        <Col
          span={6}
          order={iPositiveSpecialColor.iOrder > 24 ? 24 : iPositiveSpecialColor.iOrder}
          style={{ display: iPositiveSpecialColor.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="iPositiveSpecialColor" showDefaultName="正面专色" {...iPositiveSpecialColorProps} />
        </Col>
        <Col
          span={6}
          order={iOppositeSpecialColor.iOrder > 24 ? 24 : iOppositeSpecialColor.iOrder}
          style={{ display: 'none' }}
        >
          <ShowType sName="iOppositeSpecialColor" showDefaultName="反面专色" {...iOppositeSpecialColorProps} bVisible={false} />
        </Col>
        <Col
          span={6}
          order={dMachineWidth.iOrder > 24 ? 24 : dMachineWidth.iOrder}
          style={{ display: dMachineWidth.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="dMachineWidth" showDefaultName="宽" {...dMachineWidthProps} />
        </Col>
        <Col
          span={6}
          order={dMachineLength.iOrder > 24 ? 24 : dMachineLength.iOrder}
          style={{ display: dMachineLength.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="dMachineLength" showDefaultName="长" {...dMachineLengthProps} />
        </Col>
        <Col
          span={12}
          order={iPrintMode.iOrder > 24 ? 24 : iPrintMode.iOrder}
          style={{ display: iPrintMode.bVisible ? 'block' : 'none' }}
        >
          <ShowType sName="iPrintMode" showDefaultName="印刷方式" {...iPrintModeProps} radio />
        </Col>
      </Row>
    </Form>);
});

export default WorkOrderPpopComponent;
